<?php

namespace app\api\controller;

/**
 * Description of Sms
 *
 * @author DevPHP
 * @version 2018-4-8
 */
class Sms {

    /**
     * 发送手机登录短信验证码
     * 功能：
     * 判断手机白名单
     * 判断手机黑名单
     * 防恶意刷短信--判断IP每天最大发送次数和手机号每天最大发送次数
     * @version 2018-4-8
     */
    public function send_login_code($mobile='13950076987') {
        
        $result= \app\common\service\SmsFactory::send($mobile);
        var_export($result);
        exit;
        $ip = \think\facade\Request::ip();
        $result = \app\common\service\SmsFactory::check_filter($mobile, $ip);
        if (!$result['status']) {
            return json($result);
        }
        // 发送短信, 支持市面各大短信平台
        $result= \app\common\service\SmsFactory::send($mobile);
        $expire = 30 * 60; // 30分钟
        $code = rand(100000, 999999);
        $data = [
            'mobile' => $mobile,
            'ip' => $ip,
            'code' => $code,
            'expire' => time() + $expire,
            'status' => $result['status'],
            'remark' => $result['info'],
            'channel_name' => $result['channel_name'], // 发送短信通道(如yunpian,alisms,juhesms)
        ];
        db('sms_login_code_log')->insert();
        // 更新统计，放在定时任务中, 30秒执行一次
        db('sms_server')->where(['name' => $result['channel_name']])->inc('send_count');
        db('sms_server')->where(['name' => $result['channel_name']])->inc('send_success_count');
        db('sms_server')->where(['name' => $result['channel_name']])->inc('send_fail_count');
        if ($result['status']) {
            $return['status'] = 1;
            $result['info'] = '短信发送成功,批号[' . $result['id'] . ']';
        } else {
            $result['info'] = '短信发送失败!';
        }
        return json_encode($return);
    }

}
